package BanyuanClub.Study.Day0303;

import java.util.Arrays;

/**
 * 较快速的根据关键字查询
 * HashMap就是桶加单向链表
 */
public class MyHashMap {
    private Entry[] arr = new Entry[5];
    private int size;
    private double d = 0.75;

    public void put(Object key, Object value){
        if (size > arr.length * d)
            arr = Arrays.copyOf(arr,arr.length * 2);
        Entry entry = new Entry(key, value);
        entry.nest = arr[key.hashCode()%arr.length];
        arr[key.hashCode()%arr.length] = entry;
        size++;
    }

    public Object get(Object key){
        Entry first = arr[key.hashCode()%arr.length];
        if (first == null){
            return null;
        }
        while (first != null){
            if (first.key.equals(key))
                return first.value;
            first = first.nest;
        }
        return null;
    }

    public static void main(String[] args) {
        MyHashMap myHashMap = new MyHashMap();
        myHashMap.put("aaa","111");
        myHashMap.put("bbb","222");
        myHashMap.put("ccc","333");

    }
}

class Entry{
    public Object key;
    public Object value;
    public Entry nest;

    public Entry(Object key, Object value) {
        this.key = key;
        this.value = value;
    }
}